/*

	This file prepares regressions sample at firm-product-destination-year level

*/

cd ${work}5_reg_analysis

use jurnr vnr land year unit vrd_exp_exsmall quantity using input\export_ipdt 	///
	if year>2002 & vrd_exp_exsmall!=., clear

*PROMOTION
destring jurnr, gen(jurnr_num)
merge m:1 jurnr_num land year using input\promotion_idt, keep(1 3) keepusing(treated_2 tdcinitiative_2) nogen
drop jurnr_num
foreach v of varlist treated_2 tdcinitiative_2 {
	replace `v'=0 if `v'==.
}

*for spill-over regressions
foreach v of varlist treated_2 tdcinitiative_2 {
    bysort vnr land year : egen `v'_pdy = max(`v')
} 

*LAGGED SALES
preserve
	use input\firm_it, clear
	keep jurnr year sales
	replace sales=sales/1e6
	deflate sales
	replace year=year+1
	save temp\temp, replace
restore
merge m:1 jurnr year using temp\temp, assert(2 3) keep(3) keepusing(sales) nogen
erase temp\temp.dta

*DESTINATION INDICATORS
merge m:1 land using input\income_d, assert(2 3) keep(3) keepusing(income_group) nogen
gen dum_income = inlist(income_group,"High income","Upper middle income")
drop income_group

*ENTRY DUMMIES
preserve
	use jurnr vare land year vrd using input\export_raw_ipdt if inrange(year,2002,2015) , clear
	merge m:1 land using input\sample_d, assert(1 3) keep(3) keepusing(land) nogen
	
	destring vare, gen(cn8) force
	merge m:1 cn8 year using input\cn8_cn8plus_2002_2015, keepusing(cn8plus) keep(3) nogen
	rename cn8plus vnr
		
	collapse (sum) vrd , by(jurnr vnr land year)	
	keep if vrd>7.5
	
	egen fpd=group(jurnr vnr land)
	xtset fpd year
	gen entry=(l.fpd==.) if year>2002
	
	keep jurnr vnr land year entry
	save temp\temp, replace
restore
merge 1:1 jurnr vnr land year using temp\temp, assert(2 3) keep(3) keepusing(entry) nogen
erase temp\temp.dta
bysort jurnr land vnr entry (year): gen entry_first=1 if _n==1 & entry==1
replace entry_first=0 if entry_first==.

*PRODUCT CODES
merge m:1 vnr using input\productcodes_p, assert(2 3) keep(3) keepusing(cn2 bec_group) nogen
gen dum_bec_group = inlist(bec_group,"Capital","Intermediate") if bec_group!=""

*SPECIFICATIONS
gen log_vrd_exp_exsmall = log(vrd_exp_exsmall)
gen log_quantity = log(quantity)
gen log_price = log(vrd_exp_exsmall/quantity)

winsor2 log_vrd_exp_exsmall , cuts(0 99) replace
winsor2 log_quantity log_price , cuts(1 99) by(unit) replace

lab var log_vrd_exp_exsmall "ln(EXV)"
lab var log_quantity	"ln(EXQ)"
lab var log_price 		"ln(Price)"
 
*FEs
egen FPY=group(jurnr vnr year)
egen FPD=group(jurnr vnr land)
egen IDY=group(cn2 land year)
egen FY=group(jurnr year)
egen FD=group(jurnr land)
egen PY=group(vnr year)
egen F=group(jurnr)
drop cn2
 
*SAVE
compress
save temp\regsample_ipdt, replace 